<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  searchEngine
 */

class m_searchEngine_actions_AjaxIndexPagesAction extends m_backoffice_extends_Action
{
	public function execute()
	{
        ignore_user_abort(true);
        ini_set("memory_limit", -1);
        set_time_limit(0);
		f_core_Session::getInstance()->setValue("indexation", true);

        f_database_Provider::getInstance()->setQuery("TRUNCATE TABLE `f_index`")->execute();

        $pages = $this->getDocumentProvider()->loadDocuments("modules_website/page")->byPublicationStatus("PUBLISHED")->retrieveDocuments('*');

        foreach($pages as $page)
        {
            if($page->getRight() == "" && $page->isIndexable())
            {                
                $links = m_searchEngine_services_indexationService::getInstance($page)->getLinksToCurrentPage();

                if(count($links) > 0)
                {
                    foreach($links as $link)
                    {
                        m_searchEngine_documents_Index::createNewFromDocument($page, $link)->save();
                    }

                    m_searchEngine_documents_Index::createNewFromPage($page, true)->save();
                }
                else
                {
                    m_searchEngine_documents_Index::createNewFromPage($page)->save();
                }
            }
        }
        
		f_core_Session::getInstance()->setValue("indexation", false);

        echo 'ok';
	}	
}